package net.xisberto.phonetodesktop;

import java.util.ArrayList;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ActionMode;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class LinkListActivity extends FragmentActivity {
	private Intent service = null;
	private ArrayList<Map<String, String>> vector = null;
 
	private BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			updateList(intent);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.link_list);
		
		ListView list = (ListView) findViewById(R.id.link_list);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		vector = (ArrayList<Map<String, String>>) getLastCustomNonConfigurationInstance();
		if (vector == null) {
			showProgress();
	        service = new Intent(this, LinkListService.class);
	        service.setAction(LinkListService.LINKLIST_ACTION);
		} else {
			updateList();
		}
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if (service != null) {
			startService(service);
		}
		registerReceiver(receiver, new IntentFilter(LinkListService.LINKLIST_ACTION));
	}

	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(receiver);
		if (service != null) {
			stopService(service);
		}
	}

	@Override
	public Object onRetainCustomNonConfigurationInstance() {
		return vector;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.context_link_list, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		Intent intent = new Intent(getApplicationContext(), LinkListService.class);
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		intent.putExtra("item_index", info.position);
		switch (item.getItemId()) {
		case R.id.item_remove:
			intent.setAction(LinkListService.DELETE_LINK_ACTION);
			break;
		case R.id.item_mark_visited:
			intent.setAction(LinkListService.MARK_VISITED_ACTION);
			break;
		default:
			break;
		}
		showProgress();
		startService(intent);
		return super.onMenuItemSelected(featureId, item);
	}

	private void showProgress() {
		findViewById(R.id.link_list).setVisibility(View.GONE);
		findViewById(R.id.link_list_empty).setVisibility(View.GONE);
		findViewById(R.id.link_list_progress).setVisibility(View.VISIBLE);
	}
	
	private void showText() {
		findViewById(R.id.link_list).setVisibility(View.GONE);
		findViewById(R.id.link_list_empty).setVisibility(View.VISIBLE);
		findViewById(R.id.link_list_progress).setVisibility(View.GONE);
	}
	
	private void showList() {
		findViewById(R.id.link_list).setVisibility(View.VISIBLE);
		findViewById(R.id.link_list_empty).setVisibility(View.GONE);
		findViewById(R.id.link_list_progress).setVisibility(View.GONE);
	}
    
    public void updateList(Intent intent) {
    	boolean is_empty = intent.getBooleanExtra(LinkListService.EXTRA_IS_EMPTY, true);
    	vector = (ArrayList<Map<String, String>>) intent.getSerializableExtra(LinkListService.EXTRA_VECTOR);
    	if ((is_empty) || (vector.size() == 0)) {
    		showText();
    	} else {
    		updateList();
    	}
    	
    }
    
    private void updateList() {
    	SimpleAdapter adapter = new SimpleAdapter(
    			getApplicationContext(),
    			vector,
    			android.R.layout.simple_list_item_multiple_choice,
    			new String[] {"url"},
    			new int[] {android.R.id.text1});
		ListView list = (ListView) findViewById(R.id.link_list);
		list.setAdapter(adapter);
		
		for (int i = 0; i < vector.size(); i++) {
			Map<String, String> link = vector.get(i);
			Boolean visited = new Boolean(link.get("visited"));
			if (visited) {
				Object v = adapter.getItem(i);
				CheckedTextView text = (CheckedTextView) v;
				if (text != null)
					text.setTextColor(R.color.grass_always_regrows);
			}
		}
		
		showList();
    }

}
